package com.example.wm.SanWar.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.SanWar.VO.ChildrenVO;
import com.example.wm.SanWar.VO.CommunicationEntityVO;
import com.example.wm.SanWar.VO.NoteEntityVO;
import com.example.wm.SanWar.entity.AlliesandUser;
import com.example.wm.SanWar.entity.CommunicationEntity;
import com.example.wm.SanWar.entity.NoteDetailEntity;
import com.example.wm.SanWar.entity.NoteEntity;
import com.example.wm.SanWar.service.AlliesandUserService;
import com.example.wm.SanWar.service.CommunicationService;
import com.example.wm.SanWar.service.NoteDetailService;
import com.example.wm.SanWar.service.NoteService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;

import com.example.wm.utils.TsyUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/SanWarNoteController")
public class NoteController {
    @Resource
    private AlliesandUserService alliesandUserService;
    @Resource
    private NoteService noteService;
    @Resource
    private TsyUtils tsyUtils;
    @Resource
    private NoteDetailService noteDetailService;
    @Resource
    private CommunicationService communicationService;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody NoteEntity noteEntity) {
        noteEntity.setCreatetime(tsyUtils.getnowtime());
        noteService.save(noteEntity);

        return ResultData.success(noteEntity.getId());
    }
    @Transactional
    @PostMapping("/get")
    public Result getNotedeatil(@RequestBody JSONObject jsonObject) {
        Page<NoteEntity> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        LambdaQueryWrapper<NoteEntity> lq=new LambdaQueryWrapper<>();
        lq.orderByAsc(NoteEntity::getCreatetime);
        lq.eq(NoteEntity::getType,jsonObject.getString("type"));
        Page<NoteEntity>  list=noteService.page(pageInfo,lq);

if(list.getSize()>0){

        List<NoteEntityVO> NVOList=new ArrayList<>();


      for(NoteEntity l:list.getRecords()){
          //查询名字是否一样
          LambdaQueryWrapper<AlliesandUser> lq1=new LambdaQueryWrapper<>();
          lq1.eq(AlliesandUser::getId,l.getUserid());
          lq1.in(AlliesandUser::getStatus,"在盟","待审核");
          AlliesandUser a=alliesandUserService.getOne(lq1);
          if(a!=null){
              l.setName(a.getName());
          }

          NoteEntityVO NVO=new NoteEntityVO();
          LambdaQueryWrapper<NoteDetailEntity> lq2=new LambdaQueryWrapper<>();
          lq2.ne(NoteDetailEntity::getCategory,  "评论");
          lq2.eq(NoteDetailEntity::getType,jsonObject.getString("type"));
          lq2.eq(NoteDetailEntity::getNoteid,  l.getId());

//        lq2.orderByAsc(NoteDetailEntity::getSequencenum);
        List<NoteDetailEntity> list2=  noteDetailService.list(lq2);
        Long n=noteDetailService.count(lq2);
        l.setResoucenum(Math.toIntExact(n));
        noteService.updateById(l);


          NVO.setNoteDetailList(list2);
          NVO.setNoteEntity(l);
          NVOList.add(NVO);
      }


        return ResultData.success(NVOList);

        }else{
                 return ResultData.success("查询失败");
            }

    }
//    更新浏览量
@Transactional
@PostMapping("/updateBrowsenum")
public Result updateBrowsenum(@RequestBody NoteEntity noteEntity) {

    LambdaQueryWrapper<NoteEntity> lq=new LambdaQueryWrapper<>();
    lq.eq(NoteEntity::getId,noteEntity.getId());
  NoteEntity no=  noteService.getOne(lq);
  no.setBrowsenum(no.getBrowsenum()+1);
 Boolean b= noteService.updateById(no);
    return ResultData.success(b);
}

    @Transactional
    @PostMapping("/getCommunicate")
    public Result getCommu(@RequestBody JSONObject jsonObject) {
        Page<CommunicationEntity> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        LambdaQueryWrapper<CommunicationEntity> lq=new LambdaQueryWrapper<>();
        lq.orderByAsc(CommunicationEntity::getCommtime);
        lq.eq(CommunicationEntity::getNoteid,jsonObject.getString("nodeid"));
       Long n= communicationService.count(lq);

        Page<CommunicationEntity>  list=communicationService.page(pageInfo,lq);
        //查询用户名是否一样
        for(CommunicationEntity l:list.getRecords()){
            LambdaQueryWrapper<AlliesandUser> lq1=new LambdaQueryWrapper<>();
            lq1.eq(AlliesandUser::getId,l.getUserid());
            lq1.in(AlliesandUser::getStatus,"在盟","待审核");
            AlliesandUser a=alliesandUserService.getOne(lq1);
            if(a!=null){
                l.setName(a.getName());
            }

        }


        if(n>0){
            //更新帖子评论数量
            LambdaQueryWrapper<NoteEntity> lqnn=new LambdaQueryWrapper<>();
            lqnn.eq(NoteEntity::getId,jsonObject.getString("nodeid"));
            NoteEntity no=  noteService.getOne(lqnn);
            no.setCommnum(Math.toIntExact(n));
            noteService.updateById(no);

            List<CommunicationEntityVO> NVOList=new ArrayList<>();

        //封装数据 帖子详情 帖子父级评论 子级评论
        for(CommunicationEntity l:list.getRecords()){
            if(l.getParentsid()==0){

           CommunicationEntityVO communicationEntityVO=new CommunicationEntityVO();
                LambdaQueryWrapper<NoteDetailEntity> lq2=new LambdaQueryWrapper<>();
                lq2.eq(NoteDetailEntity::getCommid,  l.getId());
                lq2.ne(NoteDetailEntity::getCategory,  "帖子");

                lq2.orderByAsc(NoteDetailEntity::getSequencenum);
                List<NoteDetailEntity> list2=  noteDetailService.list(lq2);

                communicationEntityVO.setNoteDetailList(list2);
                communicationEntityVO.setCommunicationEntity(l);
                NVOList.add(communicationEntityVO);
            }

            //更新父级评论数量
            LambdaQueryWrapper<CommunicationEntity> lqne=new LambdaQueryWrapper<>();



            //查询子级评论
          for(CommunicationEntityVO cvo:NVOList){
              List<CommunicationEntity> categoryTree = communicationService.getCategoryTree(cvo.getCommunicationEntity());


              cvo.setChildren(categoryTree);

          }

        }


        return ResultData.success(NVOList);
}else{
    return ResultData.error(304,"查询失败");
}

    }

}
